<template>
  <div>
    <!--面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="9">
          <!-- 搜索输入框 -->
          <div>
            <el-input
              placeholder="请输入内容"
              :clearable="true"
              v-model="queryInfo.query"
              @clear="getGoodList()"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getGoodList()"
              ></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="4">
          <!-- 添加商品按钮 -->
          <div>
            <el-button type="primary" @click="showAddGoods()"
              >添加商品</el-button
            >
          </div>
        </el-col>
      </el-row>
      <!-- 商品表单数据 -->
      <el-table
        style="width: 100%"
        :border="true"
        :stripe="true"
        :data="goodsList"
      >
        <el-table-column label="#" width="50" type="index"> </el-table-column>
        <el-table-column label="商品名称" width="900" prop="goods_name">
        </el-table-column>
        <el-table-column label="商品价格（元）" width="100" prop="goods_price">
        </el-table-column>
        <el-table-column label="商品重量" width="100" prop="goods_weight">
        </el-table-column>
        <el-table-column label="创建时间" width="270" prop="upd_time">
          <template slot-scope="scope">
            {{ scope.row.upd_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row.goods_id)"
            ></el-button>

            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 编辑商品弹框 -->
    <el-dialog
      title="修改动态参数"
      :visible.sync="dialogVisibleEdit"
      width="30%"
      :model="editGoodParams"
    >
      <el-form label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="editGoodParams.goods_name" disabled> </el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="editGoodParams.goods_price"> </el-input>
        </el-form-item>
        <el-form-item label="商品重量">
          <el-input v-model="editGoodParams.goods_weight"> </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialogVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //获取商品参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 2,
      },
      goodsList: [],
      total: 10,
      //编辑商品
      editGoodParams: [],
      dialogVisibleEdit: false,
    };
  },
  created() {
    this.getGoodList();
  },
  methods: {
    //获取商品列表
    async getGoodList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      console.log(res);
      if (res.meta.status != 200)
        return this.$message.error("获取商品列表失败");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      // this.$message.success(res.meta.msg);
    },
    //每页显示量改变
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.getGoodList();
    },
    //换页
    handleCurrentChange(newpage) {
      this.queryInfo.pagenum = newpage;
      this.getGoodList();
    },
    //删除商品
    deleteGoods(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.delete("goods/" + id);
          if (res.meta.status == 400) return this.$message.error(res.meta.msg);
          if (res.meta.status !== 200) return this.$message.error("删除失败");
          this.$message.success("删除成功");
          this.getGoodList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //增加商品
    showAddGoods() {
      this.$router.push("/add");
    },
    //显示修改商品属性弹框
    async showEditDialog(id) {
      const { data: res } = await this.$http.get("goods/" + id);
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error("获取商品失败");
      this.editGoodParams = res.data;
      this.dialogVisibleEdit = true;
    },
    //发送编辑商品请求
    async edit() {
      console.log(this.editGoodParams);
      const { data: res } = await this.$http.put(
        "goods/" + this.editGoodParams.goods_id,
        this.editGoodParams
      );
      // console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("修改商品信息失败");
      this.$message.success("编辑商品信息成功");
      this.dialogVisibleEdit = false;
      this.getGoodList();
    },
  },
};
</script>
<style lang='less' scoped>
</style>